package com.studyadmin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.houbb.sensitive.word.core.SensitiveWordHelper;
import com.studyadmin.entity.Message;
import com.studyadmin.mapper.MessageMapper;
import com.studyadmin.service.MessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.studyadmin.util.JsonUtil;
import static com.studyadmin.util.SensitiveWordsUtil.textFiltering;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springfox.documentation.spring.web.json.Json;

import java.util.List;

/**
 * <p>
 * 消息表 服务实现类
 * </p>
 *
 * @author lyl
 * @since 2023-03-30
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    @Autowired
    MessageMapper messageMapper;


    /**
     * 发送消息 websocket 是否在线
     * @param message
     * @return
     */
    @Override
    public JsonUtil sendMsgSer(Message message) {
//        message.setDelFlag(0);
        JsonUtil jsonUtil = new JsonUtil();
        System.out.println("发送消息"+message);

        String content=message.getContent();
        System.out.println(SensitiveWordHelper.replace(content, '*'));
//        String str1=SensitiveWordHelper.replace(content, '0');
        message.setContent(SensitiveWordHelper.replace(content, '-'));
//        String con=textFiltering(content);
//        message.setContent(content.replace(con, "*"));
        message.setDelFlag(1);

        int res=messageMapper.insert(message);

        if(res<0){
            jsonUtil.setCode(1);
            jsonUtil.setMsg("发送失败");
        }else{
            jsonUtil.setCode(0);
            jsonUtil.setMsg("发送成功");
        }


        return jsonUtil;
    }

    /**
     * 历史记录
     * @param stuNum
     * @return
     */
    @Override
    public JsonUtil historyMsgSer(String stuNum,String receiveNum) {

        JsonUtil jsonUtil = new JsonUtil();
        List<Message> list=messageMapper.selectList(
                new QueryWrapper<Message>()
                        .and(i -> i.eq("del_flag",0))
                        .and(i -> i.eq("sender_num", Integer.parseInt(stuNum))
                                .eq("receive_num", Integer.parseInt(receiveNum)))
                        .or(i -> i.eq("sender_num", Integer.parseInt(receiveNum))
                                .eq("receive_num", Integer.parseInt(stuNum)))
        );
            if(list.size()<0){
                jsonUtil.setCode(1);
                jsonUtil.setMsg("发送失败");
            }else{
                jsonUtil.setCode(0);
                jsonUtil.setMsg("查询成功");
                jsonUtil.setData(list);
            }

        return jsonUtil;
    }

    @Override
    public JsonUtil msgReadSer(String teaNum, String stuNum) {
        JsonUtil jsonUtil = new JsonUtil();
        Message message = new Message();
        message.setDelFlag(0);

        QueryWrapper<Message> wrapper=new QueryWrapper<>();
        wrapper.eq("sender_num",stuNum)
                .eq("receive_num",teaNum);


        int result= messageMapper.update(message, wrapper);
        if(result<0){
            jsonUtil.setCode(1);
        }else {
            jsonUtil.setCode(0);
        }

        return jsonUtil;
    }
}
